package com.zoe.onelink.core.nfs.storage.impl;

import cn.hutool.core.io.FileUtil;
import com.zoe.onelink.core.nfs.storage.AbstractNfsApi;
import com.zoe.onelink.util.OsUtil;
import com.zoe.onelink.util.StrUtil;

import java.io.File;
import java.io.InputStream;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-04-22
 */
public class LocalStorageNfsImpl extends AbstractNfsApi<String> {

    private static final String ROOT_PREFIX;

    private final String localRootPath;

    static {
        ROOT_PREFIX = OsUtil.isWindows() ? System.getProperty("user.home") + "/onelink/" : "/data/";
    }

    public LocalStorageNfsImpl(String urlPrefix, String serviceName) {
        super(urlPrefix);
        this.localRootPath = ROOT_PREFIX + serviceName;
    }

    @Override
    public String uploadFile(InputStream inputStream, String path) {
        String filePath = this.localRootPath + path;
        FileUtil.writeFromStream(inputStream, new File(filePath));
        return this.getUrlPrefix() + StrUtil.prependIfMissing(path, "/");
    }

    @Override
    public InputStream downloadFile(String path) {
        return FileUtil.getInputStream(path);
    }

}
